package org.dna.wicketui.issues;


import java.util.List;
import org.apache.log4j.Logger;
import org.apache.wicket.feedback.ContainerFeedbackMessageFilter;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.dna.MetroApplication;
import org.dna.MetroSession;
import org.dna.metronomo.persistence.IIssueDAO;
import org.dna.wicketui.MetroPage;
import org.tigris.scarab.model.Issue;
import org.tigris.scarab.model.IssueType;

//TODO re-activate the menu provider
public class IssueDetailsPanel extends Panel /*implements IContextMenuProvider*/ {

    private static final long serialVersionUID = 6174871448930055063L;
    private final static Logger LOG = Logger.getLogger(IssueDetailsPanel.class);

    private boolean m_editMode = false;
    private IModel<Issue> m_issueModel;
    private Button btnSumbit;
    private TextField<String> tfDomain;
    private TextField<String> tfPrefix;
    private TextField<String> tfEmailReferences;
    private DropDownChoice<IssueType> lstIssueType;
//    private TextField<Integer> tfHours;
//    private TextField<Integer> tfMinutes;
    //this store of unboxing in necessary to avoid users the duty to insert all
    //time fields.
    protected Integer daysU;
    protected Integer hoursU;
    protected Integer minutesU;
    protected IssueType issueType;

    public IssueDetailsPanel(String id) {
        super(id);
        m_editMode = false;
        m_issueModel = new Model<Issue>(new Issue());
        initComponents();
    }
    
    /*public IssueDetailsPanel(String id, SelectableActivity editable) {
        super(id);
        m_editMode = true;
        if (editable != null) {
            m_issueModel = new LoadableActivityModel(editable);
        }
        initComponents();
    }*/

    @SuppressWarnings({"serial"})
    private void initComponents() {
        Form form = new Form("form") {

            @Override
            protected void onSubmit() {
//                if (daysU != null) {
//                    job.getElapsed().setDays(daysU);
//                }
//                if (hoursU != null) {
//                    job.getElapsed().setHours(hoursU);
//                }
//                if (minutesU != null) {
//                    job.getElapsed().setMinutes(minutesU);
//                }
//
                MetroSession session = (MetroSession)getSession();
                IIssueDAO dao = MetroApplication.get().getIssueDAO();
                
                if (m_editMode) {
//                    dao.updateJob(job);
                } else {
                    dao.insert(m_issueModel.getObject(), session.getCurrentModule(), issueType);
                }
                // navigate back to the activity details
//TODO
//                ManageIssuePage target = new ManageIssuePage();
//                target.setCurrentActivity(m_parentActModel.getObject());
//                setResponsePage(target);
                ((MetroPage)getPage()).switchToPanel(IssueListPanel.class);
            }
        };
        form.setModel(new CompoundPropertyModel(m_issueModel));
        add(form);

//        if (job.getElapsed().getDays() != 0) {
//            daysU = job.getElapsed().getDays();
//        }
//        if (job.getElapsed().getHours() != 0) {
//            hoursU = job.getElapsed().getHours();
//        }
//        if (job.getElapsed().getMinutes() != 0) {
//            minutesU = job.getElapsed().getMinutes();
//        }

        tfPrefix = new TextField<String>("prefix");
        tfDomain = new TextField<String>("domain");
        tfEmailReferences = new TextField<String>("emailReferences");
        List<IssueType> availableTypes = loadIssueTypes();
        lstIssueType = new DropDownChoice<IssueType>("type", 
                new PropertyModel<IssueType>(this, "issueType"), availableTypes,
                new ChoiceRenderer("name", "issueTypeId"));

//        tfDays = new TextField<Integer>("days", new PropertyModel(this, "daysU"));
//        tfDays.add(new MinimumValidator<Integer>(0));
//        tfHours = new TextField<Integer>("hours", new PropertyModel(this, "hoursU"));
//        tfHours.add(new RangeValidator<Integer>(0, 23));
//        tfMinutes = new TextField<Integer>("minutes", new PropertyModel(this, "minutesU"));
//        tfMinutes.add(new RangeValidator<Integer>(0, 59));
        String submitLabel = "Update";
        if (!m_editMode) {
            submitLabel = "Create";
//            tfName.setRequired(true);
            tfDomain.setRequired(true);
        }
        btnSumbit = new Button("button", new Model(submitLabel));
//        form.add(new FormComponentFeedbackBorder("border").add(tfName));
        form.add(btnSumbit);
        form.add(tfPrefix);
        form.add(tfDomain);
        form.add(tfEmailReferences);
        form.add(lstIssueType);
//        form.add(tfDays);
//        form.add(tfHours);
//        form.add(tfMinutes);
//        form.add(new AtLeastOneValidator(tfDays, tfHours, tfMinutes));

        add(new FeedbackPanel("feedback", new ContainerFeedbackMessageFilter(form)));
        LOG.info("Init form components finished in " + (m_editMode ? "edit" : "creation") + " mode");

//        List<IModel<Job>> modelJob = prepareLoadableJobListModel(m_actModel.getObject().getJobs());
//        wmc.add(new JobListPanel("jobs", modelJob));
    }

    private List<IssueType> loadIssueTypes() {
        IIssueDAO dao = MetroApplication.get().getIssueDAO();
        return dao.allTypes();
    }

//    private List<IModel<Job>> prepareLoadableJobListModel(List<Job> jobs) {
//        if (jobs == null) {
//            return new ArrayList<IModel<Job>>(); //return empty model's list
//        }
//        List<IModel<Job>> modelJobs = new ArrayList<IModel<Job>>();
//        for (Job job : jobs) {
//            modelJobs.add(new LoadableJobModel(job));
//        }
//        return modelJobs;
//    }

    //TODO reenable once the IContextMenuProvider is reactivated.
//    @SuppressWarnings("serial")
//    public List<MenuItem> prepare() {
//        MenuItem addJobItem = new MenuItem("Add Issue",
//                ManageActivityPage.class) {
//
//            @Override
//            public Component createMenu() {
//                Link link = new Link("link") {
//
//                    @Override
//                    public void onClick() {
//                        setResponsePage(new ManageIssuebPage(
//                                (SelectableActivity) m_issueModel.getObject()));
//                    }
//                };
//
//                link.add(new Label("caption", getCaption()));
//                return link;
//            }
//        };
//        return Arrays.asList(addJobItem);
//    }
}
